package com.ljh.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljh.pojo.Collection;
import com.ljh.pojo.Singer;
import com.ljh.service.CollectionService;
import com.ljh.service.SingerService;
import com.ljh.service.SongService;
import com.ljh.utils.Result;
import com.ljh.vo.SexVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/singer")
@Api(description = "歌手接口管理")
@CrossOrigin
public class singerController {

    @Autowired
    SingerService singerService;

    @Autowired
    CollectionService collectionService;

    @Autowired
    SongService songService;

    @GetMapping("/singerList")
    @ApiOperation(value = "歌手列表")
    public Result singerList(
            @RequestParam Integer current,
            @RequestParam Integer size,
            @RequestParam(required = false) String singerName
    ) {
        System.out.println("gogogo");
        IPage<Singer> page = singerService.page(new Page(current,size),new QueryWrapper<Singer>()
                .like(StrUtil.isNotBlank(singerName), "singerName",singerName));
        return Result.success(page);
    }

    @GetMapping("/deleteSingerById")
    @ApiOperation(value = "通过歌手id删除歌手")
    public Result deleteSingerById(@RequestParam Integer singerId){
        List<Collection> collByUserId = collectionService.findCollByUserId(singerId);
        for (int i =0 ;i<collByUserId.size();i++){
            Integer collId = collByUserId.get(i).getCollId();
            collectionService.deleteCollByUserId(collId);
            songService.deleteUserByCollid(collId);
        }
        singerService.deleteSingerById(singerId);
        return Result.success(1);
    }

    @GetMapping("/findSingerById")
    @ApiOperation(value = "通过歌手id来查找歌手")
    public Result findSingerById(@RequestParam Integer singerId){
        Singer singerById = singerService.findSingerById(singerId);
        return Result.success(singerById);
    }

    @PostMapping("/updateSinger")
    @ApiOperation(value = "更改歌手信息")
    public Result updateSinger(@RequestBody Singer singer){
        System.out.println(singer);
        Integer integer = singerService.updateSingerById(singer);
        if(integer!=0){
            return Result.success("修改成功");
        }else{
            return Result.fail(400, "修改失败");
        }
    }

    @GetMapping("/updatePhoto")
    @ApiOperation(value = "更新歌手照片")
    public Result UpdatePersonPhoto(@RequestParam String singerPhoto, @RequestParam Integer singerId){
        singerService.UpdatePersonPhoto(singerPhoto, singerId);
        return Result.success("修改成功");
    }

    @PostMapping("/insertSinger")
    @ApiOperation(value = "加入歌手")
    public Result insertSinger(@RequestBody Singer singer){
        System.out.println(singer);
        singerService.insertSinger(singer);
        return Result.success("添加成功");
    }

    @GetMapping("/findSingerByName")
    @ApiOperation(value = "通过歌手的名字找歌手")
    public Result findSingerByName(String singerName){
        List<Singer> singerByName = singerService.findSingerByName(singerName);
        return Result.success(singerByName);
    }

    @GetMapping("/findSingerSex")
    @ApiOperation("查找歌手的性别")
    public Result findSingerSex(){
        List<SexVo> singerSex = singerService.findSingerSex();
        return Result.success(singerSex);
    }
}
